/*
 * File:                Program.cs
 * Authors:             Jonathan T. McClain
 * Company:             Sandia National Laboratories
 * Project:             MVC
 *
 * Copyright 2008, Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000, there is a non-exclusive license for use of this work by
 * or on behalf of the U.S. Government. Export of this program may require a
 * license from the United States Government. See CopyrightHistory.txt for
 * complete details.
 *
 * Reviewers:
 * Review Date:
 * Review Comments:
 *
 * Revision History:
 *
 * $Log: Program.cs,v $
 * Revision 1.5  2008/05/08 20:56:55  jtmccl
 * Added file headers.
 *
 */

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;

namespace Sandia.MVCShell
{
    /// <summary>
    /// The main program.
    /// </summary>
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(
            string[] args)
        {
            MainForm mainForm = null;
            try
            {
                AppDomain.CurrentDomain.UnhandledException +=
                    new UnhandledExceptionEventHandler(
                        CurrentDomain_UnhandledException);
                Application.ThreadException +=
                    new ThreadExceptionEventHandler(Application_ThreadException);

                StandardAppOptions appOptions = new StandardAppOptions();
                appOptions.ProcessArgs(args);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                mainForm = new MainForm(appOptions.ApplicationTitle);
            }
            catch (Exception e)
            {
                bool rethrow =
                    ExceptionPolicy.HandleException(e, "Default Policy");
                if (rethrow)
                {
                    throw;
                }
            }
            Application.Run(mainForm);
        }

        static void Application_ThreadException(
            object sender, 
            ThreadExceptionEventArgs e)
        {
            bool rethrow =
                ExceptionPolicy.HandleException(
                e.Exception,
                "Default Policy");
            if (rethrow)
            {
                throw(e.Exception);
            }
        }

        static void CurrentDomain_UnhandledException(
            object sender, 
            UnhandledExceptionEventArgs e)
        {
            bool rethrow = 
                ExceptionPolicy.HandleException(
                e.ExceptionObject as Exception, 
                "Default Policy");
            if (rethrow)
            {
                throw(e.ExceptionObject as Exception);
            }
        }
    }
}